<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
//Includes
use_unit("menus.inc.php");
use_unit("components4phpfull/jtgroupbox.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class CompanyMeasures extends Page
{
       public $Image3 = null;
       public $cmbFuelCalculation = null;
       public $btnCancel = null;
       public $btnSubmit = null;
       public $JTGroupBox2 = null;
       public $cmbMeasure = null;
       public $JTGroupBox1 = null;
       public $Label1 = null;
       public $chkCustomField = null;
       public $JTSiteTheme1 = null;
       function btnCancelClick($sender, $params)
       {
       redirect('main.php?company_id='.$_SESSION['company_id']);
       }

       function btnSubmitClick($sender, $params)
       {
       if ($this->chkCustomField->Checked==true)
       {
       $chkCustomField ='1';
       }
       else
       {
       $chkCustomField='0';
       }

       $result=mysql_query("select * from company_measures_tbl where company_id='".$_SESSION['company_id']."'");
       $count=mysql_num_rows($result);
       if($count==0)
       {
       mysql_query("insert into company_measures_tbl (company_id,
       system_measure,
       custom_field,
       fuel_calculation)

       Values('".$_SESSION['company_id']."',
       '".$this->cmbMeasure->getItemIndex()."',
       '".$chkCustomField."',
       '".$this->cmbFuelCalculation->getItemIndex()."')") or die ("insert measures - " . mysql_error());
       }
       else
       {
       mysql_query("update company_measures_tbl set
       system_measure='".$this->cmbMeasure->getItemIndex()."',
       custom_field='".$chkCustomField."',
       fuel_calculation='".$this->cmbFuelCalculation->getItemIndex()."'
       where company_id='".$_SESSION['company_id']."'") or die ("update measures - " . mysql_error());
       }
       redirect('main.php?company_id='.$_SESSION['company_id']);
       }

       function CompanyMeasuresCreate($sender, $params)
       {
       $query = "select * from company_measures_tbl where company_id='".$_SESSION['company_id']."'";
       $result_show = mysql_query($query)or die("SELECT MEASURES - " . mysql_error());
       $row= mysql_fetch_array($result_show);

      if($row['custom_field']=='1')
      {
      $this->chkCustomField->Checked=true;
      }
      else
      {
      $this->chkCustomField->Checked=false;
      }

      $this->cmbMeasure->ItemIndex = $row['system_measure'];
      $this->cmbFuelCalculation->ItemIndex = $row['fuel_calculation'];
       }


}

global $application;

global $CompanyMeasures;

//Creates the form
$CompanyMeasures=new CompanyMeasures($application);

//Read from resource file
$CompanyMeasures->loadResource(__FILE__);

//Shows the form
$CompanyMeasures->show();

?>